/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lmx.repoDomain.repository;

import com.lmx.common.constants.ICommonConstants;
import com.lmx.common.context.ActionContextHolder;
import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.CommonService;
import com.lmx.repoDomain.tmodel.UserPrivate;
import io.katharsis.queryParams.QueryParams;
import io.katharsis.repository.annotations.JsonApiFindAll;
import io.katharsis.repository.annotations.JsonApiFindOne;
import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@JsonApiResourceRepository(UserPrivate.class)
@Component
public class UserPrivateRepository extends BaseRepository<UserPrivate>{
    private Log logger = LogFactory.getLog(this.getClass());
    @Autowired
    private CommonService commonService;

    @PostConstruct
    public void init() {
        dslJpaService.setEntityClass(UserPrivate.class);
    }

    @Override
    @JsonApiFindAll
    public Iterable<UserPrivate> findAll(final QueryParams requestParams) {
//        UserPrivate userPrivate = new UserPrivate();
//        userPrivate.setErrcode(ICommonConstants.INTERFACEERRO.NOINTERFACE.ordinal());
//        ArrayList<UserPrivate> userPrivates = new ArrayList<>();
//        userPrivates.add(userPrivate);
//        return userPrivates;
        return null;
    }


    @Override
    @JsonApiFindOne
    public UserPrivate findOne(final Long id, final QueryParams requestParams) {
        Long longId = ActionContextHolder.getContext().getUser().getId();//获取登录id
        UserPrivate t = null;
        if( longId!= null && longId.equals(id)){
            t = (UserPrivate) dslJpaService.findById(id);
            logger.debug("findOne:userPravate");
        }
        return t;
    }

    @Override
    @JsonApiSave
    public UserPrivate save(UserPrivate user) {
//        final BaseDataEntity bd = (BaseDataEntity)entity;
        if (user.getId() == null) {
            user.setErrcode(ICommonConstants.INTERFACEERRO.NOINTERFACE.ordinal());//禁止创建
        } else {
//            user.setPassword(null);//禁止修改密码
            String action = ActionContextHolder.getContext().getAction();
            if(StringUtils.isNotBlank(action) && "applyVerify".equals(action)){
                String content = "";
                if(user.getRole().getId() == commonService.getCSRole().getId()){
                    logger.debug("客服申请认证。");
                    content += "客服：" + user.getName();
                }else if(user.getRole().getId() == commonService.getEntRole().getId()){
                    logger.debug("企业申请认证。");
                    content += "企业：" + user.getEntName();
                }
                content += "<br>" + "loginName：" + user.getLoginName()
                        + "<br>" + "申请信息认证"
                        + "<br>" + "申请时间：" + CommonUtil.getCurrTimeForString();

                commonService.operateRemind("用户申请认证",content);
                user = (UserPrivate)dslJpaService.update(user);
            }else if(StringUtils.isNotBlank(action) && "bind".equals(action)){

            }else{
                user = (UserPrivate)dslJpaService.update(user);
            }

        }
        user.setPassword(null);//屏蔽敏感信息
        return user;
    }

}
